//
//  ViewController.m
//  ScrollViewDemo
//
//  Created by Lucky on 16/3/19.
//  Copyright © 2016年 Lucky. All rights reserved.
//

#import "ViewController.h"
#import "JKRTableViewController.h"
#import "JKRLabel.h"

#define screenW [UIScreen mainScreen].bounds.size.width

@interface ViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *titleScrollView;

@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
//    self.contentScrollView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    
    [super viewDidLoad];

    [self setUpChildViewController];
    
    [self setupContent];
    
    [self setupTitle];
    
    [self scrollViewDidEndScrollingAnimation:self.contentScrollView];
}

- (void)setUpChildViewController
{
    JKRTableViewController *vc0 = [[JKRTableViewController alloc] init];
    vc0.title = @"国际";
    [self addChildViewController:vc0];
    
    JKRTableViewController *vc1 = [[JKRTableViewController alloc] init];
    vc1.title = @"军事";
    [self addChildViewController:vc1];
    
    JKRTableViewController *vc2 = [[JKRTableViewController alloc] init];
    vc2.title = @"社会";
    [self addChildViewController:vc2];
    
    JKRTableViewController *vc3 = [[JKRTableViewController alloc] init];
    vc3.title = @"政治";
    [self addChildViewController:vc3];
    
    JKRTableViewController *vc4 = [[JKRTableViewController alloc] init];
    vc4.title = @"经济";
    [self addChildViewController:vc4];
    
    JKRTableViewController *vc5 = [[JKRTableViewController alloc] init];
    vc5.title = @"体育";
    [self addChildViewController:vc5];
    
    JKRTableViewController *vc6 = [[JKRTableViewController alloc] init];
    vc6.title = @"娱乐";
    [self addChildViewController:vc6];
}

- (void)setupTitle
{
    CGFloat labelW = 100;
    CGFloat labelY = 0;
    CGFloat labelH = self.titleScrollView.frame.size.height;
    
    for (NSInteger i = 0; i < 7; i++) {
        JKRLabel *label = [[JKRLabel alloc] init];
        label.text = [self.childViewControllers[i] title];
        CGFloat labelX = i * labelW;
        label.frame = CGRectMake(labelX, labelY, labelW, labelH);
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)]];
        label.tag = i;
        [self.titleScrollView addSubview:label];
        
        UIViewController *willShowViewController = self.childViewControllers[i];

        CGFloat width = screenW;
        CGFloat height = [UIScreen mainScreen].bounds.size.height;
        CGFloat offsetX = i * width;

        willShowViewController.view.frame = CGRectMake(offsetX, 0, width, height);
        [self.contentScrollView addSubview:willShowViewController.view];

        if (i == 0) {
            label.scale = 1.0;
        }
    }
    
    self.titleScrollView.contentSize = CGSizeMake(labelW * 7, 0);
}

- (void)setupContent
{
    self.contentScrollView.contentSize = CGSizeMake(7 * screenW, 0);
}

- (void)labelClick:(UITapGestureRecognizer *)tap
{
    NSInteger index = tap.view.tag;
    
    CGPoint offset = self.contentScrollView.contentOffset;
    
    offset.x = index * screenW;
    
    [self.contentScrollView setContentOffset:offset animated:YES];
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSLog(@"%s", __func__);
    
    CGFloat width = scrollView.frame.size.width;
    CGFloat offsetX = scrollView.contentOffset.x;
    
    NSInteger index = offsetX / width;
    
    JKRLabel *label = self.titleScrollView.subviews[index];
    CGPoint titleOffset = self.titleScrollView.contentOffset;
    titleOffset.x = label.center.x - self.titleScrollView.frame.size.width * 0.5;
    
    if (titleOffset.x < 0) {
        titleOffset.x = 0;
    }
    
    CGFloat maxOffset = self.titleScrollView.contentSize.width - self.titleScrollView.frame.size.width;
    
    if (titleOffset.x > maxOffset) {
        titleOffset.x = maxOffset;
    }
    
    [self.titleScrollView setContentOffset:titleOffset animated:YES];
    
    for (JKRLabel *otherLabel in self.titleScrollView.subviews) {
        if (otherLabel != label) {
            otherLabel.scale = 0.0;
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"%s", __func__);
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x < 0) {
        return;
    }
    
    CGFloat scale = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    NSInteger leftIndex = scale;
    JKRLabel *leftLabel = self.titleScrollView.subviews[leftIndex];
    
    CGFloat rightScale = scale - leftIndex;
    CGFloat leftScale = 1 - rightScale;
    
    if (leftIndex == self.titleScrollView.subviews.count - 1) {
        return;
    }
    
    NSInteger rightIndex = scale + 1;
    JKRLabel *rightLabel = self.titleScrollView.subviews[rightIndex];
    
    leftLabel.scale = leftScale;
    rightLabel.scale = rightScale;

}

@end
